package org.lonetree.tc.webapp.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.lonetree.tc.core.Book;
import org.lonetree.tc.core.Comment;
import org.lonetree.tc.core.Section;
import org.lonetree.tc.core.Book.Isbn;
import org.lonetree.tc.core.dao.BookDAO;
import org.lonetree.tc.core.dao.DAOFactory;

import com.amazon.webservices.awsecommerceservice._2007_10_29.AWSECommerceServiceClient;
import com.amazon.webservices.awsecommerceservice._2007_10_29.AWSECommerceServicePortType;
import com.amazon.webservices.awsecommerceservice._2007_10_29.ItemLookup;
import com.amazon.webservices.awsecommerceservice._2007_10_29.ItemLookupRequest;
import com.amazon.webservices.awsecommerceservice._2007_10_29.ItemLookupResponse;

public class ViewBookDetails extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		
		try{
			Book book;
			book = (Book)request.getAttribute("book");
			if(book == null){
				Isbn isbn=new Isbn(request.getParameter("isbn"));
				DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.FactoryImplementation.DB_DAO_FACTORY);
				BookDAO bdao = factory.getBookDAO();
				book=bdao.getBook(isbn);
			}
			
			Amazon a=new Amazon(request.getParameter("isbn").trim());
			AmazonBook aa=new AmazonBook();

			AWSECommerceServiceClient client=new AWSECommerceServiceClient();
	        String accessKeyId = "1B6E1HZ70T3DR6Y2AWR2";
	        String associateTag = "winaffiliate";
	        ItemLookup itemLookup = new ItemLookup() ;
	        itemLookup.setAssociateTag(associateTag);
	        itemLookup.setAWSAccessKeyId(accessKeyId);
	        
	        ItemLookupRequest request = new ItemLookupRequest();
	        request.setIdType("ISBN");
	        request.setSearchIndex("Books");
	        java.util.List<ItemLookupRequest> list = itemLookup.getRequest();
	        list.add(request);
	        java.util.List<String> id = request.getItemId();
	        id.add(request.getParameter("isbn"));
	        java.util.List<String> responseGroup = request.getResponseGroup();
	        responseGroup.add("Medium");
	        AWSECommerceServicePortType p = client.getAWSECommerceServicePort() ;
			ItemLookupResponse response = p.itemLookup(itemLookup);
			
			aa.setBookUrl(response.getItems().get(0).getItem().get(0).getDetailPageURL());
			aa.setLargeImageURL(response.getItems().get(0).getItem().get(0).getLargeImage().getURL());
			aa.setMediumImageURL(response.getItems().get(0).getItem().get(0).getMediumImage().getURL());
			aa.setPrice(response.getItems().get(0).getItem().get(0).getOfferSummary().getLowestNewPrice().getFormattedPrice());
			aa.setSmallImageURL(response.getItems().get(0).getItem().get(0).getSmallImage().getURL());
			aa.setTitle(response.getItems().get(0).getItem().get(0).getItemAttributes().getTitle());
			
			
			request.setAttribute("AmazonBook", aa);
			request.setAttribute("book", book);
			request.setAttribute("sections", book.getSections());
			request.setAttribute("sources", book.getSources().iterator());	
			request.setAttribute("comments", book.getComments().iterator());	
			return mapping.findForward("success");
		}
		catch (Exception e)
		{
			
		}
			return mapping.findForward("failure");
	}

}
